package edu.fmi.st.epf.types.skill.ui.provider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.epf.library.edit.IDefaultNameSetter;
import org.eclipse.epf.library.edit.ILibraryItemProvider;
import org.eclipse.epf.library.edit.IStatefulItemProvider;
import org.eclipse.epf.library.edit.category.CustomCategoryItemProvider;
import org.eclipse.epf.library.edit.internal.IListenerProvider;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.uma.ContentPackage;
import org.eclipse.epf.uma.CustomCategory;
import org.eclipse.epf.uma.MethodElement;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.swt.graphics.Image;

import edu.fmi.st.epf.types.skill.common.util.SkillHelper;
import edu.fmi.st.epf.types.skill.ui.SkillActivator;

public class BaseSkillsItemProvider extends CustomCategoryItemProvider
		implements ILibraryItemProvider, IStatefulItemProvider,
		IDefaultNameSetter, IListenerProvider {

	private static final String NEW_NAME = "new_skill";
	private static final String NEW_GROUP_NAME = "new_skill_group";

	public static void setDefaultName(Object obj, Notifier target) {
		if (obj instanceof CustomCategory) {
			CustomCategory cat = (CustomCategory) obj;
			if (SkillHelper.isSkill(cat)) {
				@SuppressWarnings("unchecked")
				final List<CustomCategory> onlyProjectPractices = TngUtil
						.extract(
								((ContentPackage) target).getContentElements(),
								CustomCategory.class);
				TngUtil.setDefaultName(onlyProjectPractices,
						(MethodElement) obj, NEW_NAME);
			} else if (SkillHelper.isSkillGroup(cat)) {
				@SuppressWarnings("unchecked")
				final List<CustomCategory> onlyProjectPractices = TngUtil
						.extract(
								((ContentPackage) target).getContentElements(),
								CustomCategory.class);
				TngUtil.setDefaultName(onlyProjectPractices,
						(MethodElement) obj, NEW_GROUP_NAME);
			} else {
				@SuppressWarnings("unchecked")
				final List<CustomCategory> onlyProjectPractices = TngUtil
						.extract(
								((ContentPackage) target).getContentElements(),
								CustomCategory.class);
				TngUtil.setDefaultName(onlyProjectPractices,
						(MethodElement) obj, "tralala");
			}
		}
	}

	public static String getCreateChildText(Object obj) {
		if (SkillHelper.isSkill(obj)) {
			return SkillHelper.MAKE_SKILL_TEXT;
		} else if (SkillHelper.isSkillGroup(obj)) {
			return SkillHelper.MAKE_GROUP_TEXT;
		}
		throw new IllegalStateException("Unknown child type for Sills: " + obj);
	}

	public boolean isWrappingNeeded(Object object) {
		return super.isWrappingNeeded(object);
	}

	public BaseSkillsItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public Collection<?> getChildren(Object object) {
		// XXX Set the childrenStoreMap to null, to get the updated content, how
		// to listen (add adapter) on custom category for changes?!
		childrenStoreMap = null;
		return super.getChildren(object);
	}

	@Override
	public Collection<? extends EStructuralFeature> getChildrenFeatures(
			Object object) {
		if (childrenFeatures == null) {
			childrenFeatures = new ArrayList<EStructuralFeature>();
			childrenFeatures.add(UmaPackage.eINSTANCE
					.getContentPackage_ContentElements());
		}
		return childrenFeatures;
	}

	@Override
	public Image getImage(Object object) {
		return SkillActivator.getDefault().getImage("full/obj/Skill"); //$NON-NLS-1$
	}

	@Override
	public void setDefaultName(Object obj) {
		setDefaultName(obj, target);
	}

	@Override
	public String getCreateChildText(Object owner, Object feature,
			Object child, Collection<?> selection) {
		return getCreateChildText(child);
	}

	public void setAdapterFactory(AdapterFactory adapterFactory) {
		if (adapterFactory == null) {
			throw new NullPointerException("Adapter factory should not be null");
		}
		if (this.adapterFactory != null) {
			throw new IllegalStateException(String.format(
					"Adapter factory already set: %s, new: %s",
					this.adapterFactory, adapterFactory));
		}
		this.adapterFactory = adapterFactory;
	}

}
